package com.shop.controller;

import com.shop.entity.PageResult;
import com.shop.entity.Result;
import com.shop.pojo.Product;
import com.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired private ProductService productService;

    @RequestMapping("/findAllSellProduct")
    @ResponseBody
    /*查询所有上架的商品*/
    public PageResult findAllSellProduct(
            Product product,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "9") int pageSize) {
        PageResult pageResult = null;
        try {
            pageResult = productService.findAllSellProduct(product, pageNum, pageSize);
        } catch (Exception e) {
            e.getMessage();
        }
        return pageResult;
    }

    @RequestMapping("/findAllNoSellProduct")
    @ResponseBody
    /*查询所有下架的商品*/
    public PageResult findAllNoSellProduct(
            Product product,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "9") int pageSize) {
        PageResult pageResult = null;
        try {
            pageResult = productService.findAllNoSellProduct(product, pageNum, pageSize);
        } catch (Exception e) {
            e.getMessage();
        }
        return pageResult;
    }

    @RequestMapping("/addProduct")
    @ResponseBody
    /*2.添加商品*/
    public Result addProduct(Product product) {
        Result result = null;
        try {
            result = productService.addProduct(product);
        } catch (Exception e) {
            result = new Result(false, e.getMessage());
        }
        return result;
    }

    @RequestMapping("/deleteProduct")
    @ResponseBody
    /*3.删除商品*/
    public Result deleteProduct(String id) {
        Result result = null;
        try {
            result = productService.deleteProduct(id);
        } catch (Exception e) {
            result = new Result(false, e.getMessage());
        }
        return result;
    }

    @RequestMapping("/updateProduct")
    @ResponseBody
    /*4.修改商品*/
    public Result updateProduct(Product product) {
        Result result = null;
        try {
            result = productService.updateProduct(product);
        } catch (Exception e) {
            result = new Result(false, e.getMessage());
        }
        return result;
    }

    @RequestMapping("/findProductById")
    @ResponseBody
    /*5.查找单个商品*/
    public Product findProductById(String pid) {
        Product product = null;
        try {
            product = productService.findProductById(pid);
        } catch (Exception e) {
            e.getMessage();
        }
        return product;
    }

    @RequestMapping("/fuzzySearchProduct")
    @ResponseBody
    /*6.模糊查找商品（根据名称模糊查找）*/
    public PageResult fuzzySearchProduct(
            Product product,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "9") int pageSize) {
        PageResult pageResult = null;
        try {
            pageResult = productService.fuzzySearchProduct(product, pageNum, pageSize);
        } catch (Exception e) {
            e.getMessage();
        }
        return pageResult;
    }

    @RequestMapping("/putAwayProduct")
    @ResponseBody
    /*7.商品上架*/
    public Result putAwayProduct(Product product) {
        Result result = null;
        try {
            result = productService.putAwayProduct(product);
        } catch (Exception e) {
            result = new Result(false, e.getMessage());
        }
        return result;
    }

    @RequestMapping("/soldOutProduct")
    @ResponseBody
    /*8.商品下架*/
    public Result soldOutProduct(Product product) {
        Result result = null;
        try {
            result = productService.soldOutProduct(product);
        } catch (Exception e) {
            result = new Result(false, e.getMessage());
        }
        return result;
    }
}
